package com.pie4cloud.pie.job.biz.service;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.pie4cloud.pie.common.core.util.RedisUtil;
import com.pie4cloud.pie.job.api.entity.DmzEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class NettyService {
	/*@Autowired
	private NacosServiceManager nacosServiceManager;
	@Autowired
	private NacosDiscoveryProperties nacosDiscoveryProperties;*/
	@Autowired
	private RedisUtil redisUtil;
	private static String monitorDetail = "MONITOR_DETAIL";


	public DmzEntity getServiceInfo() {
		List<DmzEntity> dmzEntities=new ArrayList<>();
		Map<Object, Object> map = redisUtil.hmget(monitorDetail);
		for (Object o:map.values()) {
               String str= (String) o;
			   DmzEntity dmzEntity= JSON.parseObject(str,DmzEntity.class);
			   if(dmzEntity.getSystemInfoDto().getIsUsable()==0){
				   dmzEntities.add(dmzEntity);
			   }
		}
		int size = dmzEntities.size();
		Random random = new Random();
		DmzEntity dmzEntity = dmzEntities.get(random.nextInt(size));
		return dmzEntity;
	}
}
		//NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
		/*try {
				List<Instance> instances = namingService.getAllInstances("pie-dmz");
				int size = instances.size();
				Random random = new Random();
				Instance instance = instances.get(random.nextInt(size));
				return instance;
			} catch (NacosException e) {
				e.printStackTrace();
			}
			return null;
			}
		}*/
